﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Text;
using System.Xml;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using EQT.Model;

namespace EQT.Dal
{
    public partial class DaSamplekeptlog
    {
        /// <summary>
        /// 分页查询样品保管记录。
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="TotalCount"></param>
        /// <returns></returns>
        public DataSet GetSampleKeptLogRecords(string condition, int PageSize, int PageIndex, out int TotalCount)
        {

            string strGetSampleKeptLogRecords = "select * from V_SAMPLE_KEPT_LOG ";

            if (!string.IsNullOrEmpty(condition))
            {
                strGetSampleKeptLogRecords = strGetSampleKeptLogRecords + condition;
            }

            try
            {
                return DaCommon.GetPaginationData(strGetSampleKeptLogRecords, PageSize, PageIndex, " ID desc", out TotalCount);

            }
            catch (DbException ex)
            {
                HandleDbException(ex);
            }
            TotalCount = 0;
            return new DataSet();
        }

        /// <summary>
        /// 获取所有未注销的样品登记信息。
        /// </summary>
        /// <returns></returns>
        public DataSet GetDistinctSampleName()
        {
            string strGet = "select * from Sample_Kept where Sample_Status<>'注销'";

            try
            {
                return db.ExecuteDataSet(CommandType.Text, strGet);

            }
            catch (DbException ex)
            {
                HandleDbException(ex);
            }
            return new DataSet();
        }

        /// <summary>
        /// 获取所有未注销的样品登记信息。
        /// </summary>
        /// <returns></returns>
        public DataSet GetSampleKeptLog(string id)
        {
            string strGet = "select * from V_SAMPLE_KEPT_LOG where id='"+id+"'";

            try
            {
                return db.ExecuteDataSet(CommandType.Text, strGet);

            }
            catch (DbException ex)
            {
                HandleDbException(ex);
            }
            return new DataSet();
        }

        /// <summary>
        /// 增加样品交接登记，同时把上次交接记录状态改为 已交接。
        /// </summary>
        /// <param name="mod"></param>
        /// <returns></returns>
        public int AddExchange(MoSamplekeptlog mod)
        {
            int  rc = 0;
            using (DbConnection cnn = db.CreateConnection())
            {
                DbTransaction trans = null;
                try
                {
                    cnn.Open();
                    using (trans = cnn.BeginTransaction())
                    {
                        //修改样品的其他登记为 已交接。
                        string sql = "update SampleKeptLog set Status='已交接' where Sam_Kep_ID='" + mod.Sam_Kep_Id + "' and Status='在管'";
                        db.ExecuteNonQuery(trans,CommandType.Text, sql);
                        //增加交接日志。
                        this.Add(mod, trans);
                        trans.Commit();
                    }
                    cnn.Close();
                    rc = 1;
                }
                catch (DbException ex)
                {
                    if (trans != null) trans.Rollback();
                    HandleDbException(ex);
                }
            }
            return rc;
        }
    }
}
